package com.serkol.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Primary
public class MyMetaObjectHandler implements MetaObjectHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(MyMetaObjectHandler.class);

    @Override
    public void insertFill(MetaObject metaObject) {
        LOGGER.info("start insert fill ....");
        this.setFieldValByName("deleted", 0, metaObject);
        this.strictInsertFill(metaObject, "createDate", Date.class, new Date());
        this.setFieldValByName("createDate", new Date(), metaObject);
        this.strictInsertFill(metaObject, "updateDate", Date.class, new Date());
        this.setFieldValByName("updateDate", new Date(), metaObject);
        this.setFieldValByName("date", new Date(), metaObject);

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("date",new Date(), metaObject);
        //this.strictInsertFill(metaObject, "updateDate", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "updateDate", Date.class, new Date());
        this.setFieldValByName("updateDate", new Date(), metaObject);
        this.setFieldValByName("updateDate", new Date(), metaObject);
    }



}
